﻿namespace WebApplication1
{
    public class LocalFileLogger : ILogger
    {
        private readonly string categoryName;
        private readonly string basePath;
        public LocalFileLogger(string categoryName)
        {
            this.categoryName = categoryName;
            string[] fieldstrs = Enum.GetNames(typeof(LogLevel));
            foreach (var item in fieldstrs)
            {
                basePath = Directory.GetCurrentDirectory().Replace("\\", "/") + "/Logs/"+$"/{item}/";

                if (Directory.Exists(basePath) == false)
                {
                    Directory.CreateDirectory(basePath);
                }
            }
            basePath = Directory.GetCurrentDirectory().Replace("\\", "/") + "/Logs/";
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return default!;
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            if (logLevel != LogLevel.None)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
        {
            if (IsEnabled(logLevel))
            {
                if (state != null && state.ToString() != null)
                {
                    var logContent = state.ToString();

                    if (logContent != null)
                    {
                        if (exception != null)
                        {
                            var logMsg = new
                            {
                                message = logContent,
                                error = new
                                {
                                    exception?.Source,
                                    exception?.Message,
                                    exception?.StackTrace
                                }
                            };

                            logContent = System.Text.Json.JsonSerializer.Serialize(logMsg);
                        }

                        var log = new
                        {
                            CreateTime = DateTime.UtcNow,
                            Category = categoryName,
                            Level = logLevel.ToString(),
                            Content = logContent
                        };

                        string logStr = System.Text.Json.JsonSerializer.Serialize(log);

                        string logPath=string.Empty;
                         switch (logLevel)
                        {
                            case LogLevel.Trace:
                             logPath= basePath + "/Trace/"+ DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            case LogLevel.Debug:
                                logPath = basePath + "/Debug/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            case LogLevel.Information:
                                logPath = basePath + "/Information/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            case LogLevel.Warning:
                                logPath = basePath + "/Warning/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            case LogLevel.Error:
                                logPath = basePath + "/Error/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            case LogLevel.Critical:
                                logPath = basePath + "/Critical/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            case LogLevel.None:
                                logPath = basePath + "/None/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                            default:
                                logPath = basePath + "/Start/" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                                break;
                        }         
                          File.AppendAllText(logPath, logStr + Environment.NewLine, System.Text.Encoding.UTF8);
                    }
                   
                   
                }
            }
        }
    }
}
